package com.plc.demo.plctest;

import snap7.Snap7Exception;
import snap7.SrvEvent;
import snap7.SrvEventCallback;
import snap7.SrvEventFlags;
import snap7.system.Srv;

public class S7200SmartSimulator {

    public static void main(String[] args) {
        SrvEventCallback callback = new SrvEventCallback() {
            @Override
            public void eventNotification(SrvEvent event) {
                System.out.println("事件通知: " + event.toString());
            }
        };

        for (int i = 1; i <= 255; i++) {
            String ip = "192.168.3." + i;
            startServer(ip, callback);
        }
    }

    public static void startServer(String ipAddress, SrvEventCallback callback) {
        Srv server = new Srv();
        try {
            server.startTo(ipAddress, 0, 1, 102); // 启动服务器，监听指定IP和端口
            server.setEventsCallback(callback, SrvEventFlags.RemoteConnect | SrvEventFlags.RemoteDisconnect);
            System.out.println("服务器启动成功，监听地址: " + ipAddress + ":102");
        } catch (Snap7Exception e) {
            System.out.println("无法启动服务器监听 " + ipAddress + ":102");
            e.printStackTrace();
        }

        // 模拟数据块DB1
        byte[] db1 = new byte[1024]; // 假设DB1有1024个字节
        try {
            server.createDataBlock(1, db1); // 创建DB1数据块
            System.out.println("数据块DB1创建成功");
        } catch (Snap7Exception e) {
            System.out.println("无法创建数据块DB1");
            e.printStackTrace();
        }

        // 你可以在这里添加更多的数据块和逻辑来模拟PLC的行为
    }
}
